package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.AccountLog;
import com.example.entity.Admin;
import com.example.entity.Agent;
import com.example.entity.AgentFund;
import com.example.pojo.dto.SurchargeDto;
import com.example.pojo.enums.FundType;
import com.example.pojo.feign.AgentFeignService;
import com.example.pojo.request.FundOperateRequest;
import com.example.pojo.response.Result;
import com.example.service.IAgentService;
import com.example.service.impl.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/xc")
public class OKRAccountController {


    @Autowired
    private IAgentService agentService;
    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private AgentFeignService feignService;


   /* *//*查询单个下游渠道详情*//*
    @GetMapping("/loadAgent/{id}")
    public Result loadAgent(@PathVariable("id") ){
    }
    */

    /*查询下游渠道列表*/  //代理商id 名称 编号
    @GetMapping("/loadList")
    public Result loadList(){
        List<Agent> agents = agentService.list();
        return Result.success(agents);
    }

    /*传入的账户信息 - 款项详情
     * 代理id   操作人员phone  审核人员phone  凭证(图片或文件地址)  金额
     * */
    /*渠道加款*/
    @PostMapping("/surcharge")
    public Result channelSurcharge(@RequestBody SurchargeDto surchargeDto){

        //String auditorPhone = surchargeDto.getAuditorPhone();
        //String voucher = surchargeDto.getVoucher();
        String agentCode = surchargeDto.getAgentCode();
        String operatorPhone = surchargeDto.getOperatorPhone();
        String remark = surchargeDto.getRemark();
        BigDecimal price = surchargeDto.getPrice();
        String agentOrderNo = surchargeDto.getAgentOrderNo();

        LambdaQueryWrapper<Agent> dbAgent = new LambdaQueryWrapper<>();
        dbAgent.eq(Agent::getAgentCode,agentCode);
        Agent agent = agentService.getOne(dbAgent);
        if (agent==null){
            return Result.error("未查询到渠道");
        }
        Integer agentId = agent.getId();
        BigDecimal newPrice = agent.getPrice().add(price);
        agent.setPrice(newPrice);
        AgentFund agentFund = new AgentFund();//审核明细
        AccountLog accountLog = new AccountLog();
        accountLog.setAgentId(agentId);
        accountLog.setAgentName(agent.getAgentName());
        accountLog.setAgentOrderNo(agentOrderNo);
        accountLog.setLogType("Agent");
        accountLog.setBeforePrice(agent.getPrice());//操作前
        accountLog.setAfterPrice(newPrice);//操作后
        accountLog.setType("AddFunds");
        accountLog.setChangePrice(price);

        agentFund.setAgentId(agentId);
        agentFund.setAgentName(agent.getAgentName());
        //agentFund.setFundStatus(); 凭证
        agentFund.setNotes(remark);

        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin ::getPhone, operatorPhone);
        Admin admin = adminService.getOne(wrapper);
        if (admin==null){
            agentFund.setOperateId(-1);
        }else {
            agentFund.setOperateId(admin.getId());
        }

        agentFund.setPrice(price);
        agentFund.setToExamineId(0);
        agentFund.setType(FundType.AddFunds);
        agentFund.setFundStatus(2);
        //调用agent-service
        FundOperateRequest operateRequest = new FundOperateRequest();
        operateRequest.setFund(agentFund);
        operateRequest.setAgent(agent);
        operateRequest.setAccountLog(accountLog);
        feignService.channelPayment(operateRequest);
        return Result.success("同步成功");
    }
}
